<?php
/**
 * 服务
 */
namespace app\mobile\controller;

use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use dragon\Entity\Suggestion;
use dragon\Entity\Rescue;
use dragon\SysConfig;
use think\Validate;

class ServiceController extends BaseController
{
    // 求救
    public function rescue() {
        $data = $this->request->post();
        $data['user_id'] = $this->user['id'];
        $data['user_name'] = $this->user['user_realname'] ?: $this->user['user_nickname'];
        $data['user_mobile'] = $this->user['mobile'];

        Rescue::create($data);

        return $this->success('发送成功，请保持手机畅通，等待救援!');
    }

    // 投诉建议
    public function suggestion_add() {
        $data = $this->request->post();

        $validate = new Validate([
            'title' => 'require',
            'content' => 'require',
            'type' => 'require|number',
        ]);
        $validate->message([
            'title.require'   => '标题不可为空',
            'content.require' => '内容不可为空',
            'type.require' => '请选择类型',
            'type.number' => '类型错误',
        ]);

        $result = $validate->check($data);

        if ($result !== true) {
            $this->error($validate);
        }

        $data['user_id'] = $this->user['id'];
        $data['user_name'] = $this->user['user_realname'] ?: $this->user['user_nickname'];
        $data['user_mobile'] = $this->user['mobile'];

        Suggestion::create($data);

        // 发送消息
        message($this->user['id'], '投诉建议提醒', '您的意见我们已经收到，感谢您的反馈！');

        return $this->success('提交成功，感谢您的反馈!');
    }


    // 指南
    public function suggestion() {
        $list = Suggestion::where(['user_id' => $this->user['id']])->order('id desc')->paginate($this->perpage);

        foreach($list as $key => $val) {
            $list[$key]['type_format'] = dict_convert('suggestion_type', $val['type']);
            $list[$key]['create_time_format'] = time_format('m-d H:i', $val['create_time']);
        }

        return $this->success($list);
    }

    // 指南
    public function guide() {
        $param = $this->request->param();
        $param['post_type'] = SysConfig::TYPE_GUIDE;

        $postService = new PostService();
        $list        = $postService->adminArticleList($param);

        $list->appends($param);

        foreach($list as $key => $val) {
            $list[$key]['published_time_format'] = time_format('m-d', $val['published_time']);
            $list[$key]['create_time_format'] = time_format('m-d H:i', $val['create_time']);
            $list[$key]['update_time_format'] = time_format('m-d H:i', $val['update_time']);
        }

        return $this->success($list);
    }

    public function guideInfo() {
        $id = $this->request->param('id', 0, 'intval');

        $portalPostModel = new PortalPostModel();
        $info = $portalPostModel->where('id', $id)->find();
        $info['published_time_format'] = time_format('m-d H:i', $info['published_time']);
        $info['create_time_format'] = time_format('m-d H:i', $info['create_time']);
        $info['update_time_format'] = time_format('m-d H:i', $info['update_time']);

        return $this->success($info);
    }


}
